﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace OneMake.Model
{
    //全网买家规模分析
    public class report_qw_customer_scale_stat
    {
        public List<string> titles { get; set; }
        public Dictionary<DateTime, Dictionary<string, int>> values { get; set; }
        public report_qw_customer_scale_stat()
        {
            titles = new List<string>();
            values = new Dictionary<DateTime, Dictionary<string, int>>();
        }
    }

    public class report_qw_customer_scale_stat2
    {
        public DateTime stat_datetime { get; set; }
        public string category { get; set; }
        public int value { get; set; }
    }

    partial class ReportHelper
    {
        public List<string> get_report_qw_customer_scale_stat_titles(DateTime beginTime, DateTime endTime)
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            string sql = "select stat_datetime, category from mf_m_mjgmfx where stat_datetime between @0 and @1 group by category";
            IDataReader reader = db.ExecuteReader(sql, beginTime, endTime);

            List<string> titles = new List<string>();
            titles.Add("日期");
            while (reader.Read())
            {
                string title = reader["category"].ToString();
                titles.Add(title);
            }
            return titles;
        }

        public report_qw_customer_scale_stat get_report_qw_customer_scale_stat(DateTime beginTime, DateTime endTime)
        {
            var db = new PetaPoco.Database("mysql" + UserSession.Network);
            string sql = @"select stat_datetime,category,SUM(n_products) as value from mf_m_mjgmfx 
                        where stat_datetime BETWEEN @0 AND @1
                        group by stat_datetime,category ";
            IEnumerable<report_qw_customer_scale_stat2> items = db.Query<report_qw_customer_scale_stat2>(sql, beginTime, endTime);

            report_qw_customer_scale_stat report = new report_qw_customer_scale_stat();
            report.titles.Add("日期");

            foreach (report_qw_customer_scale_stat2 item in items)
            {
                report.titles.Add(item.category);

                if (!report.values.ContainsKey(item.stat_datetime))
                    report.values.Add(item.stat_datetime, new Dictionary<string, int>());

                Dictionary<string, int> selValues = report.values[item.stat_datetime];
                if (selValues.ContainsKey(item.category))
                    selValues[item.category] += item.value;
                else
                    selValues.Add(item.category, item.value);
            }

            return report;
        }
    }
}
